package uk.ac.durham.tel.synergynet.ats.subjects.handlers;

import java.util.Iterator;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import uk.ac.durham.tel.synergynet.ats.subjects.model.SubjectNode;
import uk.ac.durham.tel.synergynet.ats.subjects.model.SubjectRootNode;

public class DeleteSubjectHandler extends AbstractHandler implements IHandler {

	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (!(selection instanceof IStructuredSelection))
			return null;
		IStructuredSelection structSel = (IStructuredSelection) selection;
		for(Iterator<?> i = structSel.iterator(); i.hasNext();) {
			Object element = i.next();
			if (element instanceof SubjectNode) {
				((SubjectRootNode) ((SubjectNode) element).getParent()).removeChildCollectionNode((SubjectNode) element);
			}
		}
		return null;
	}

}
